import {onMounted, ref} from "vue";
import {onBeforeRouteUpdate, useRoute} from "vue-router";
import {getCategoryAPI} from "@/apis/category.js";

export function useCategory(){
    const categoryData = ref({})
    const route = useRoute()
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id)
        categoryData.value = res.result
    }
    onMounted(()=>getCategory())
// 路由变化时，分类数据接口重新发送
    onBeforeRouteUpdate((to)=>{
        console.log('路由变化了:' + to)
        getCategory(to.params.id)
    })
    return {
        categoryData
    }
}

